<?php

namespace app\admin\controller\exam;

use app\common\controller\Backend;

/**
 * 学生成绩报告分析
 *
 * @icon fa fa-circle-o
 */
class Report extends Backend
{

    /**
     * Exam模型对象
     * @var \app\common\model\exam\Student
     */
    protected $model = null;
    protected $noNeedRight = ['detail'];

    protected $searchFields = '';

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\common\model\exam\Report;
    }

    public function detail($ids = null)
    {
        $exam = \app\common\model\exam\Exam::get($ids);

        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax()) {

            if (!$exam) {
                $result = array("total" => 0, "rows" => []);
                return json($result);
            }

            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $total = $this->model
                ->where($where)
                ->where('exam_id', $ids)
                ->order($sort, $order)
                ->count();

            $list = $this->model
                ->where($where)
                ->where('exam_id', $ids)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();

            if ($list) {
                $list = collection($list)->toArray();

//                $student_ids = array_column($list, 'student_id');
//                $students = \app\common\model\Student::alias('student')
//                    ->join('__CLASSES__ classes', 'classes.id = student.class_id')
//                    ->join('__GRADE__ grade', 'grade.id = student.grade_id')
//                    ->whereIn('student.id', $student_ids)
//                    ->column('student.id,student.name,classes.name class_name,grade.name grade_name');
//
//                foreach ($list as $key => $value) {
//                    if (isset($students[$value['student_id']])) {
//                        $list[$key] += $students[$value['student_id']];
//                    }
//                }
            }

            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }

        return $this->view->fetch();
    }


}
